package com.terren.spider.core.html.scheme.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.terren.spider.entity.core.Entry;
import com.terren.spider.util.common.StringUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.utils.UrlUtils;

public class MaitianErshoufangScheme  extends BasicCustomScheme{

	@Override
	public Set<String> findHelpPaginationUrl(Page page, Entry entry) {
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(findPagination(page, entry)); // 分页部分
		return resultSet;
	}

	/**
	 * 分页
	 * @param page
	 * @param entry
	 * @return
	 */
	private Set<String> findPagination(Page page, Entry entry) {
		List<String> pgList = new ArrayList<String>();// 存放分页list
		String lastUrl = "";
		//#paging > a:nth-child(10)
		List<String> pageUrls = page.getHtml().$("#paging").links().all();//判断是否有分页，获取当前页面所有分页链接
		List<String> lastUrls = page.getHtml().$("#paging > a:nth-child(10)").links().all();//获取尾页的url
		if (pageUrls == null || pageUrls.size()< 2) {
			pgList.addAll(pageUrls);
		}
		if(lastUrls != null && lastUrls.size() !=0 ){
			lastUrl = lastUrls.get(0);
		}
		if (!"".equals(lastUrl)) {
			String pageNum = StringUtil.getNumbers(lastUrl); 	
			Integer totalPage = Integer.parseInt(pageNum.trim());
			String domain = UrlUtils.getDomain(entry.getEntryUrl());
			for (int i = 2; i <= totalPage; i++) {
				pgList.add("http://"+domain + "/esfall/PG"+i+"/");
			}
		}
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(pgList);
		resultSet.remove(null);
		return resultSet;
	}
}
